package helpdesk;

import com.mongodb.DB;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.table.AbstractTableModel;

/**
 * @author Chris
 */
public class StudentGUI extends javax.swing.JFrame
{
    ArrayList<Announcement> announcementData;
    User loggedInStudent = null;
    DB db = MongoDB.getConnection();
    
    /**
     * Creates new form StudentGUI2
     */
    public StudentGUI()
    {
        initComponents();
        this.setVisible(true);
    }
    
    public StudentGUI(User student)
    {
        announcementData = MongoDB.getRecentAnnouncements();
        this.loggedInStudent = student;
        
        initComponents();
        //This sets the table so user can view posts in a forum table by looping
        announcementTable.getColumnModel().getColumn(0).setHeaderValue("Author");
        announcementTable.getColumnModel().getColumn(1).setHeaderValue("Title");
        announcementTable.getColumnModel().getColumn(2).setHeaderValue("Body");
        
        for(int i = 0; i < announcementData.size(); i++)
        {
            announcementTable.setValueAt(announcementData.get(i).author, i, 0);
            announcementTable.setValueAt(announcementData.get(i).title, i, 1);
            announcementTable.setValueAt(announcementData.get(i).body, i, 2);
        }
        
        this.setVisible(true);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        studentPostRole = new javax.swing.JComboBox();
        studentSubmitQueryButton = new javax.swing.JButton();
        studentTitle = new javax.swing.JLabel();
        studentUploadFormsButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        announcementTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        studentViewForumButton = new javax.swing.JButton();
        studentExitButton = new javax.swing.JButton();
        graduateAssistantViewDocumentsButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        studentPostRole.setMaximumRowCount(2);
        studentPostRole.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Submit to Graduate Office", "Submit to All" }));
        studentPostRole.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                studentPostRoleActionPerformed(evt);
            }
        });

        studentSubmitQueryButton.setText("Submit Query");
        studentSubmitQueryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                studentSubmitQueryButtonActionPerformed(evt);
            }
        });

        studentTitle.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        studentTitle.setText("Student Page");

        studentUploadFormsButton.setText("Upload Forms");
        studentUploadFormsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                studentUploadFormsButtonActionPerformed(evt);
            }
        });

        announcementTable.setModel(new AbstractTableModel()
            {
                @Override
                public int getRowCount()
                {
                    return announcementData.size();
                }

                @Override
                public int getColumnCount()
                {
                    return 3;
                }

                @Override
                public String getValueAt(int i, int j)
                {
                    String temp = null;
                    switch (j)
                    {
                        case 0:
                        temp = announcementData.get(i).author;
                        break;
                        case 1:
                        temp = announcementData.get(i).title;
                        break;
                        case 2:
                        temp = announcementData.get(i).body;
                        break;
                    }
                    return temp;
                }

                public void setValueAt(String val, int i, int j)
                {
                    switch (j)
                    {
                        case 1:
                        announcementData.get(i).author = val;
                        break;
                        case 2:
                        announcementData.get(i).title = val;
                        break;
                        case 3:
                        announcementData.get(i).body = val;
                        break;
                    }
                }
            });
            jScrollPane1.setViewportView(announcementTable);

            jLabel1.setText("Announcements");

            studentViewForumButton.setText("View Forum");
            studentViewForumButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    studentViewForumButtonActionPerformed(evt);
                }
            });

            studentExitButton.setText("Exit");
            studentExitButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    studentExitButtonActionPerformed(evt);
                }
            });

            graduateAssistantViewDocumentsButton.setText("View Uploaded Documents");
            graduateAssistantViewDocumentsButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    graduateAssistantViewDocumentsButtonActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(52, 52, 52)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(studentExitButton)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 427, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createSequentialGroup()
                            .addGap(195, 195, 195)
                            .addComponent(studentTitle))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(234, 234, 234)
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(graduateAssistantViewDocumentsButton))
                            .addGroup(layout.createSequentialGroup()
                                .addContainerGap()
                                .addComponent(studentPostRole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(24, 24, 24)
                                .addComponent(studentSubmitQueryButton)
                                .addGap(18, 18, 18)
                                .addComponent(studentUploadFormsButton)
                                .addGap(18, 18, 18)
                                .addComponent(studentViewForumButton))))
                    .addContainerGap(19, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(studentTitle)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(studentUploadFormsButton)
                        .addComponent(studentPostRole, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(studentSubmitQueryButton)
                        .addComponent(studentViewForumButton))
                    .addGap(23, 23, 23)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(graduateAssistantViewDocumentsButton))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                    .addComponent(studentExitButton)
                    .addGap(22, 22, 22))
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents

    private void studentPostRoleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_studentPostRoleActionPerformed

    }//GEN-LAST:event_studentPostRoleActionPerformed

    private void studentSubmitQueryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_studentSubmitQueryButtonActionPerformed
        new AddPost(loggedInStudent, studentPostRole.getSelectedIndex());
        this.dispose();
    }//GEN-LAST:event_studentSubmitQueryButtonActionPerformed
    //This button press opens a directory viewer for students to upload forms
    //where only PDF files are viewable, and saves the file directory to the database
    private void studentUploadFormsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_studentUploadFormsButtonActionPerformed
        JFileChooser chooser = new JFileChooser();
        chooser.setAcceptAllFileFilterUsed(false);
        ExtensionFileFilter filter1 = new ExtensionFileFilter("PDF", new String[] { "PDF", "pdf"});
        chooser.setFileFilter(filter1);
        chooser.showDialog(this, "Open");
        File fileObj = chooser.getSelectedFile();
        GFS.saveFile(fileObj, loggedInStudent.username, "forms", db); 
    }//GEN-LAST:event_studentUploadFormsButtonActionPerformed
    //This button press action opens the forum and 
    //displays the database list of posts in a table view
    private void studentViewForumButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_studentViewForumButtonActionPerformed
        ArrayList<Post> forumPosts = MongoDB.getRecentPosts(loggedInStudent);

        ForumGUI table = new ForumGUI(forumPosts, loggedInStudent);
        
        table.setVisible(true);
    }//GEN-LAST:event_studentViewForumButtonActionPerformed
    //This button press opens a new login screen and closes the student GUI
    private void studentExitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_studentExitButtonActionPerformed
        Login loginWindow = new Login();
        this.dispose();
    }//GEN-LAST:event_studentExitButtonActionPerformed
    //This button press opens a new window to view documents that have been uploaded
    private void graduateAssistantViewDocumentsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_graduateAssistantViewDocumentsButtonActionPerformed
        new ViewDocGUI(GFS.retrieveAllDocs("forms", MongoDB.getConnection()), "forms");
    }//GEN-LAST:event_graduateAssistantViewDocumentsButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(StudentGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(StudentGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(StudentGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(StudentGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new StudentGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable announcementTable;
    private javax.swing.JButton graduateAssistantViewDocumentsButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton studentExitButton;
    private javax.swing.JComboBox studentPostRole;
    private javax.swing.JButton studentSubmitQueryButton;
    private javax.swing.JLabel studentTitle;
    private javax.swing.JButton studentUploadFormsButton;
    private javax.swing.JButton studentViewForumButton;
    // End of variables declaration//GEN-END:variables
}
